package com.csw.clazzTeacher.service;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csw.clazzTeacher.dao.UserDAO;
import com.csw.clazzTeacher.entity.User;

/**
 * @author 1
 */
@Service("userService")
@Transactional(rollbackFor = Exception.class)
public class UserSerivceImpl implements UserService {

	@Autowired
	private UserDAO userDAO;

	// 注册用户
	@Override
	public void save(User user) {
		try {
			user.setId(UUID.randomUUID().toString());
			user.setCreateDate(new Date());
			userDAO.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("注册用户信息失败");
		}
	}

	// 用户登陆
	@Override
	public User login(User user) {
		// 根据用户名查询用户
		User user1 = userDAO.queryUser(user.getUsername());
		// 判断用户是否存在
		if (user1 != null) {
			// 比较密码
			if (user1.getPassword().equals(user.getPassword())) {
				return user1;
			} else {
				throw new RuntimeException("密码输入错误~~");
			}
		} else {
			throw new RuntimeException("用户名输入错误~~");
		}
	}

}
